package com.yangfan.screenshot.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.yangfan.screenshot.service.ScreenShotService;

/**
 * @author : yangfan
 * @date : 2022/12/15-12:02
 * desc   : 获取截屏权限
 */
public class MediaProjectActivity extends Activity {

    private final String TAG = MediaProjectActivity.class.getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MediaProjectionManager mediaProjectionManager = (MediaProjectionManager)getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(),200);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                Intent intent = new Intent(getApplicationContext(), ScreenShotService.class);
                intent.putExtra("intent_data",data);
                intent.putExtra("intent_code",resultCode);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    getApplicationContext().startForegroundService(intent);
                } else {
                    getApplicationContext().startService(intent);
                }
            }
            finish();
        }

        overridePendingTransition(0,0);
    }
}
